package com.alan.springbootbase.controller;


import com.alan.springbootbase.model.User;
import com.alan.springbootbase.service.RedisService;
import com.alan.springbootbase.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Slf4j
@RestController
@RequestMapping("/redis")
public class RedisController {

    @Value("${spring.profiles.active}")
    private String active;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RedisService redisService;


    @RequestMapping("/hello1")
    public String index() {
        log.debug("debug Hello World");
        return "Hello World "+active;
    }



    @RequestMapping("/set")
    public String set() {
        log.debug("------------- Redis String start -----------------");

        //String
        redisUtil.set("11111","str111111");
        redisUtil.set("22222","str22222");
        //Redis中文件夹分组
        redisUtil.set("aaa:bbb:111","str222221");
        redisUtil.set("aaa:bbb:222","str222222");
        redisUtil.set("aaa:ccc:111","str222223");
        redisUtil.set("aaa:ccc:333","str222224");

        log.debug("Redis判断："+redisUtil.hasKey("11111"));    //true
        log.debug("Redis判断："+redisUtil.hasKey("aaa:bbb:111"));   //true
        log.debug("Redis判断："+redisUtil.hasKey("aaa"));          //false
        log.debug("Redis判断："+redisUtil.hasKeys("aaa"));          //true


        log.debug("------------- Redis Hash start -----------------");

        Map<String, Object> map = new HashMap<>();
        map.put("code", "s001");
        map.put("name", "zzzzz");
        map.put("phone", "18600001111");
        map.put("age", "20");
        map.put("sex", "1");
        redisUtil.hmset("hmset111",map);
        redisUtil.hset("hmset111","name","zzzaaa");
        redisUtil.hset("hmset111","id","123456");

        log.debug("------------- Redis List start -----------------");

        redisUtil.del("list001");

        List<Object> userList = new ArrayList<Object>();
        User user =new User();
        user.setId("1");
        user.setCode("001");
        user.setName("zhangsan");
        user.setAge(1);
        userList.add(user);
        User user1 =new User();
        user1.setId("2");
        user1.setCode("002");
        user1.setName("lisi");
        user1.setAge(22);
        userList.add(user1);
        redisUtil.listSet("list001",userList);
        User user2 =new User();
        user2.setId("3");
        user2.setCode("003");
        user2.setName("wangwu");
        user2.setAge(32);
        userList.add(user2);
        redisUtil.lSet("list001",user2);

        redisUtil.lGetListSize("list001");

        log.debug("------------- Redis Set start -----------------");



        log.debug("------------- Redis Z Set start -----------------");





        log.debug("debug Hello World");
        return "Hello World "+active;
    }





    @RequestMapping("/save")
    public String save() {
        log.debug("CacheManger Redis  save...");
        User user=new User();
        user.setId("12345");
        redisService.sava(user);
        return "Hello World "+active;
    }


    @RequestMapping("/update")
    public String update() {
        log.debug("CacheManger Redis  update...");
        User user=new User();
        user.setId("12345");
        redisService.update(user);
        return "Hello World "+active;
    }


    @RequestMapping("/find")
    public String findOne() {
        log.debug("CacheManger Redis  findOne...");
        redisService.findById("12345");
        return "Hello World "+active;
    }

    @RequestMapping("/del")
    public String del() {
        log.debug("CacheManger Redis  del...");
        redisService.del("12345");
        return "Hello World "+active;
    }


}
